/* 
Scenic Graphics Library
Copyright (C) 2007 Jouni Tulkki

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USApackage scenic;
*/

package scenic.toolkit;

import java.awt.geom.Point2D;

import scenic.Gradient;
import scenic.LinearColorFunction;
import scenic.Color;
import scenic.Graphics;
import scenic.geom.TransformBuilder;
import scenic.toolkit.db.event.ChangeEvent;
import scenic.toolkit.event.MouseEvent;
import scenic.toolkit.event.MouseListener;
import scenic.toolkit.event.MouseWheelEvent;

public class CButton extends CComponent {
	private CComponent content;
	private boolean mouseInside;
	private boolean buttonDown;
	
	private class ButtonMouseListener implements MouseListener {

		public void mouseClicked(MouseEvent e) {
			System.out.println("Button clicked (button = " + CButton.this + ", x = " + e.getPosition().getX() + ", y = " + e.getPosition().getY() + ")");
		}

		public void mouseEntered(MouseEvent event) {
			System.out.println("Button entered (button = " + CButton.this + ")");
			mouseInside = true;
			invalidate();
		}

		public void mouseExited(MouseEvent event) {
			System.out.println("Button leaved (button = " + CButton.this + ")");
			mouseInside = false;
			invalidate();
		}

		public void mousePressed(MouseEvent event) {
			System.out.println("Button pressed (button = " + CButton.this + ")");
			buttonDown = true;
			event.consume();
			invalidate();
		}

		public void mouseReleased(MouseEvent event) {
			buttonDown = false;
			invalidate();
		}

		public void mouseWheelMoved(MouseWheelEvent event) {
		}		
	}
	
	
	public CButton(CComponent content) {
		setContent(content);
		addMouseListener(new ButtonMouseListener());
	}
	
	public void setContent(CComponent content) {
		removeChild(this.content);
		this.content = content;
		addChild(this.content);
	}
	
	public void paint(Graphics g) {
		double w = getWidth();
		double h = getHeight();
		double r = 5.0;
		// Color color = new Color(0.9f, 1.8f, 0.9f);
		
		Gradient gradient = new Gradient();
		LinearColorFunction f = new LinearColorFunction();
		Color color = new Color(0.7f, 0.7f, 1.0f).multiply(1.4f);
		
		if(buttonDown) {
			f.addNode(0, color.multiply(0.8f));			
			f.addNode(1, color.multiply(0.9f));
		} else if(mouseInside) {
			f.addNode(0, color.multiply(0.9f));
			f.addNode(1, color.multiply(0.8f));
		} else {
			f.addNode(0, color.multiply(0.9f));
			f.addNode(1, color.multiply(0.7f));			
		}
		gradient.setTransform(new TransformBuilder().scaleBy(1, h).rotateBy(90.0));
		gradient.setType(Gradient.LINEAR);
		gradient.setFunction(f);
		
		g.clear();
		g.roundedRectangle(0.5, 0.5, w - 1.0, h - 1.0, r);
		g.setFillBrush(gradient);
		if(mouseInside)
			g.setStrokeColor(new Color(0.6f));
		else
			g.setStrokeColor(new Color(0.3f, 0.3f, 0.3f));
		g.setLineWidth(1.0);
		g.fillAndStroke();
		
		f = new LinearColorFunction();
		f.addNode(0, new Color(0.2f, 0.2f, 0.2f, 0.0f));
		f.addNode(1, new Color(0.05f, 0.05f, 0.05f, 0.0f));
		
		gradient = new Gradient();
		gradient.setTransform(new TransformBuilder().scaleBy(1, 10).rotateBy(90.0));
		gradient.setType(Gradient.LINEAR);
		gradient.setFunction(f);
		g.roundedRectangle(2.0, 1.0, w - 4, 15, 3);
		g.setFillBrush(gradient);
		g.fill();
		
		content.setPosition(r, r, getWidth() - r * 2, getHeight() - r * 2);
		g.add(content.getScene());
/*		
		g.clear();
		g.roundedRectangle(0.5, 0.5, w - 1, h - 1, 5);
		if(mouseInside)
			g.setFillColor(new ScenicColor(0.9f));
		else
			g.setFillColor(new ScenicColor(0.8f));
		g.setStrokeColor(new ScenicColor(0.0f));
		g.fillAndStroke();
		g.translate(r, r); 
		content.setHeight(h - r * 2);
		content.setWidth(w - r * 2);
		g.add(content.getScene()); */
	}	
	
	public Point2D getMinimumSizeFor(double width, double height) {
		double r = 5.0;
		Point2D s = content.getMinimumSizeFor(width - r * 2, height - r * 2);
		
		return new Point2D.Double(s.getX() + r * 2, s.getY() + r * 2);
	}
}
